<?php
class StoreController extends CommonController{
	private $model;
	public function __init(){
		parent::__init();
		$this->model=K('Store');
	}
//1. 店铺首页
	public function index(){
		$data=K('StoreCatCitView')->get_data();
		// p($data);die;
		$this->assign('data',$data);
		$this->display();
	}
	// 增加
	public function add(){
		// 分类框
		$cateData = K('Category')->get_data();  
        $this->assign('cateData',$cateData);
        // 城市分类
		$cityData=K('City')->get_data();
        $cityData=Data::tree($cityData,'cityname','ctid','ct_pid');
        $this->assign('cityData',$cityData);     
        
        if(IS_POST){
            if(!$this->model->create()) $this->error($this->model->error);
        	// p($_POST);die;
        	$data=$this->model->add_data();
        	if($data){
        		$this->success('添加商铺成功','Store/index');
        	}else{
                $this->error('添加失败,请上传商铺图片');
            }
        }
		$this->display();
	}
	// 显示店铺详细信息
	public function show_all(){
		$sid=Q('get.sid',0,'intval');
		$data=K('StoreCatCitView')->get_one_data($sid);
		// p($data);die;
		$this->assign('data',$data);
		$this->display();
	}
	//修改店铺
	public function edit(){
		// 分类框
		$cateData = K('Category')->get_data();  
        $this->assign('cateData',$cateData);
        // 城市分类
		$cityData=K('City')->get_data();
        $cityData=Data::tree($cityData,'cityname','ctid','ct_pid');
        $this->assign('cityData',$cityData);
        // 当前店铺
		$sid=Q('get.sid',0,'intval');
		$oldData=K('StoreCatCitView')->get_one_data($sid);
		// p($oldData);die;
		$this->assign('oldData',$oldData);

		if(IS_POST){
        	// p($_POST);die;
        	$data=$this->model->update_data($sid);
        	if($data){
        		$this->success('修改成功','Store/index');
        	}
        }
		$this->display();
	}
	// 删除店铺
	public function dele(){
        $sid=Q('get.sid',0,'intval');
        $data=$this->model->dele($sid);
        if($data){
        	$this->success('删除成功',U('Store/index'));
        }
	}

//2. 菜类
    public function type(){
    	$sid=Q('get.sid',0,'intval');
    	$data=K('Type')->get_data(array('bd_store_sid'=>$sid));

    	$this->assign('sid',$sid);
    	$this->assign('data',$data);

    	$this->display();
    }
    public function type_add(){   	
    	// p($sid);die;
    	if(IS_POST){
    		$sid=Q('get.sid',0,'intval');
    		// p($sid);die;
    		$type_data=array(
                'tname'=>Q('post.tname'),
                'is_show'=>Q('post.is_show',0,'intval'),
                'order'=>Q('post.order',0,'intval'),
                'bd_store_sid'=>$sid,
    		);
    	    $data=K('Type')->add_data($type_data);
    	    if($data){
    		    $this->success('添加菜类成功',U('Store/type',array('sid'=>$sid)));
    	    }
        }
    	$this->display();
    }
    public function type_dele(){
    	$tid=Q('get.tid',0,'intval');
    	// p($tid);die;
    	$data=K('Type')->dele($tid);
        if($data){
        	$this->success('删除成功');
        }
    }
    public function type_edit(){
    	// 菜类id
    	$tid=Q('get.tid',0,'intval');
    	$oldData=K('Type')->get_one_data($tid);
        // 当前店铺id
    	$sid=$oldData['bd_store_sid'];
        $this->assign('oldData',$oldData);
        if(IS_POST){    	
        	$data=K('Type')->update_data($tid);
        	if($data){
        		$this->success('修改成功',U('Store/type',array('sid'=>$sid)));
        	}
        }
    	$this->display();
    }
// 3.菜品
    public function goods(){
    	$tid=Q('get.tid',0,'intval');
    	$data=K('Goods')->get_data(array('bd_type_tid'=>$tid));

    	$this->assign('tid',$tid);
    	$this->assign('data',$data);

    	$this->display();
    }
    public function goods_add(){
    	$tid=Q('get.tid',0,'intval');
    	if(IS_POST){
            if(!K('Goods')->create()) $this->error(K('Goods')->error);
            // 图片上传处理
            $up=new Upload();
            $upInfo=$up->upload();
            p($upload);die;
            if(!$upInfo){$this->error('error');}
            $picture=$upInfo[0]['path'];
            // 各项重组
    		$good_data=array(
                'gname'=>Q('post.gname'),
                'old_price'=>Q('post.old_price',0,'intval'),
                'new_price'=>Q('post.new_price',0,'intval'),
                'goods_picture'=>$picture,
                'bd_type_tid'=>$tid,
    		);
            // 修改数据
    		$data=K('Goods')->add_data($good_data);
    		if($data){
    			$this->success('添加菜品成功',U('Store/goods',array('tid'=>$tid)));
    		}else{
                $this->error('添加失败,请上传菜品图片');
            }
    	}	
    	$this->display();
    }
    public function goods_edit(){
    	$gid=Q('get.gid',0,'intval');    	    
    	$oldData=K('Goods')->get_one_data($gid);
    	$tid=$oldData['bd_type_tid'];
    	$this->assign('oldData',$oldData);
    	if (IS_POST) {
            $data=K('Goods')->update_data($gid);
            if($data){
            	$this->success('修改成功',U('Store/goods',array('tid'=>$tid)));
            }
    	}
        $this->display();
    }
    public function goods_dele(){
    	$gid=Q('get.gid',0,'intval'); 
    	$data=K('Goods')->dele($gid);
    	if($data){
    		$this->success('删除成功');
    	}
    }
}